//
//  SLDaySelectAlertView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/8/2.
//

#import "SLDaySelectAlertView.h"
#import "SLDaySelectCell.h"

static  NSString *const kCellId = @"SLDaySelectCell";
@interface SLDaySelectAlertView()<UITableViewDelegate,UITableViewDataSource>{
    
    
}
@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;

@end

@implementation SLDaySelectAlertView
-(void)awakeFromNib{
    [super awakeFromNib];
    _bgView.layer.cornerRadius = 10;
    _bgView.layer.masksToBounds = YES;
    _titleLbl.text = kLocalString(@"Day Select");
    _tableBgView.layer.cornerRadius = 20;
    _tableBgView.layer.masksToBounds = YES;
    _dataArray = @[kLocalString(@"Monday"),kLocalString(@"Tuesday"),kLocalString(@"Wednesday"),kLocalString(@"Thursday"),kLocalString(@"Friday"),kLocalString(@"Saturday"),kLocalString(@"Sunday")];
    [self.tableBgView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
}

- (IBAction)doneAction:(id)sender {
    if (_selects.count == 0) {
        kShowToast(@"Please select day!");
        return;
    }
    if (self.btnBlock) {
        self.btnBlock(_selects,1);
    }
}

-(void)setSelects:(NSMutableArray *)selects{
    _selects = [[NSMutableArray alloc] initWithArray:selects];
    [_tableView reloadData];
}

- (IBAction)cancelAction:(id)sender {
    if (self.btnBlock) {
        self.btnBlock(_selects,0);
    }
}

#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLDaySelectCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    NSString *title = _dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLbl.text = title;
    if ([_selects containsObject:title]) {
        cell.icon.hidden = NO;
        cell.titleLbl.textColor = KColor_O;
    }else{
        cell.icon.hidden = YES;
        cell.titleLbl.textColor = KWhiteColor_text;
    }
    cell.LINE.hidden = indexPath.row == _dataArray.count-1;
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        headview.contentView.backgroundColor = KClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *title = _dataArray[indexPath.row];
    if ([_selects containsObject:title]) {
        [_selects removeObject:title];
    }else{
        [_selects addObject:title];
    }
    [_tableView reloadData];
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 50;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}


@end
